﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpTrainer.Util;
using SharpTrainer.Objects;
using Zeus.Thunder.Core;

namespace SharpTrainer.Other
{
    class JsonSerializeTest : ITestCase
    {
        public void Run()
        {
            string jsonA = "{ \"IDA\": 1, \"NameA\": \"A\", \"B\": { \"IDB\": 2, \"NameB\": \"B\" } }";
            string jsonB = "{ \"IDB\":1, \"NameB\": \"B\" }";
            string jsonC = "[{ \"IDB\":1, \"NameB\": \"B\" }, { \"IDB\":2, \"NameB\": \"BB\" }]";
            
            Console.WriteLine("----- DataContractJsonSerializer -----");     
       
            ModelA a1 = JsonSerializer.Json2Model<ModelA>(jsonA);
            ModelB b1 = JsonSerializer.Json2Model<ModelB>(jsonB);            
            Output(a1);            
            Output(b1);            
                
            //Console.WriteLine("----- JavaScriptSerializer -----");    
        
            //ModelA a3 = JsonSerializer.Json2ModelJS<ModelA>(jsonA);            
            //ModelB b3 = JsonSerializer.Json2ModelJS<ModelB>(jsonB);            
            //Output(a3);            
            //Output(b3);            
        }

        private void Output(ModelA a)        
        {
            Console.WriteLine("** Model A **");
            Console.WriteLine("IDA:"+a.IDA + "， NameA:" + a.NameA);            
            if (a.B != null)            
            {                
                Console.WriteLine("B:" + a.B.IDB + "，" + a.B.NameB);            
            }        
        }        
        
        private void Output(ModelB b)        
        {
            Console.WriteLine("** Model B **");
            Console.WriteLine("IDB:" + b.IDB + "， NameB:" + b.NameB);        
        }
    }
}
